package com.example.gaodetest.slice;


import com.amap.adapter.graphics.Color;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.autonavi.base.amap.mapcore.maploader.AMapLoader;
import com.example.gaodetest.ResourceTable;
import com.example.gaodetest.bean.LocationBean;
import com.example.gaodetest.locatorcallback.MylocatorCallBack;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.render.render3d.resources.ResourceHandle;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;
import ohos.global.resource.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.hiviewdfx.ResourceTag;
import ohos.location.Location;
import ohos.location.Locator;
import ohos.location.RequestParam;

import java.io.IOException;


public class MainAbilitySlice extends AbilitySlice {

    //地图视图
    private MapView mapView;

    //地图控制器
    private AMap aMap;


    // 定位器
    private Locator locator;



    //上报设备定位信息
    private MylocatorCallBack mylocatorCallBack = new MylocatorCallBack();

    //定位权限匹配值
    public static final int MY_LOCATION_PERMISSION = 6;

    private static final HiLogLabel hilog = new HiLogLabel(HiLog.DEBUG, 0x0000, "APP_LOG");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //显示地图首页
        initMapView();


    }

    // 1.显示地图
    public void initMapView() {
        //1.1.地图显示
        mapView = new MapView(this);

        mapView.onCreate(null);
        mapView.onResume();
        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
        mapView.setLayoutConfig(config);
        super.setUIContent(mapView);

        aMap = mapView.getMap();

        //1.2.申请定位权限
        requestLocationPermission();
    }

    // 2.申请"位置"权限
    public void requestLocationPermission(){
        if (verifySelfPermission("ohos.permission.LOCATION") != IBundleManager.PERMISSION_GRANTED ){

            if (canRequestPermission("ohos.permission.LOCATION") ){
                //是否可以申请"位置"的动态弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(
                   new String[]{"ohos.permission.LOCATION","ohos.permission.LOCATION_IN_BACKGROUND"}, MY_LOCATION_PERMISSION
                );
            }else{
                //提示用户进入设置界面进行授权
                new ToastDialog(getContext()).setText("请进入手机系统【设置】中，开启应用的定位权限").show();
            }
        }else{
            //此应用已授予权限
            new ToastDialog(getContext()).setText("此应用已授权,可进行定位功能的初始化").show();
            HiLog.info(hilog,"此应用已授权，开始定位" );
            //通过开启定位开关，显示定位蓝点
            isOpenLocationSwitch();
        }
    }

    // 3.是否开启"位置信息"开关
    public void isOpenLocationSwitch(){
        HiLog.info(hilog,"获取定位器" );
        locator = new Locator(getContext());

        //判断“位置信息”开关是否已开启
        if(locator.isLocationSwitchOn()){
            HiLog.info(hilog,"[位置信息]开关已开启" );
            onLocationChangeListener();
        } else {
            new ToastDialog(getContext()).setText("请在控制中心打开【位置信息】开关，以便获取当前所在位置").show();
            //结束定位
            locator.stopLocating(mylocatorCallBack);
        }
    }



    //4.获取手机定位
    public void onLocationChangeListener() {

        HiLog.info(hilog,"定位功能开始" );
        // 定位类型，选择"导航场景"
        RequestParam requestParam = new RequestParam(RequestParam.SCENE_NAVIGATION);

        HiLog.info(hilog,"开启定位" );
        // 开启定位
        locator.startLocating(requestParam,mylocatorCallBack);

        // 绘制蓝点
        HiLog.info(hilog,"绘制蓝点");
        getMyLocationStyle();
    }


    // 5.将定位点绘制成蓝点
    private void getMyLocationStyle(){
        HiLog.info(hilog,"绘制定位小蓝点" );
        MyLocationStyle locationStyle;
        locationStyle = new MyLocationStyle();//初始化定位蓝点样式
        locationStyle.interval(2000);//设置连续定位模式下的的定位间隔，值在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒
        locationStyle.showMyLocation(true);
        locationStyle.strokeColor(Color.BLUE);
        locationStyle.radiusFillColor(Color.RED);
        locationStyle.strokeWidth(20);


        int iconID = ResourceTable.Id_locationblue;
        HiLog.info(hilog,"iconID:" +  iconID);
        locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(iconID));
        HiLog.info(hilog,"BitmapDescriptorFactory错误。。。" );



        locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);
        aMap.setMyLocationStyle(locationStyle); //设置定位蓝点的style
        aMap.getUiSettings().setMyLocationButtonEnabled(false); // 设置默认定位按钮是否显示，非必须设置

        locationStyle.anchor(0.0f,1.0f);
        aMap.setMyLocationEnabled(true);//设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false

        //设置地图缩放比例
        LatLng latLng = new LatLng(22.61317778046695,114.04109666755971);
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));


        aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                HiLog.info(hilog,"绘制定位小蓝点，开始。。。。" );

            }
        });


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }



    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null) {
            mapView.onDestroy();
        }
    }


}
